import json
from urllib.parse import urlencode, parse_qs
from urllib.request import urlopen

import logging
from django.conf import settings
from itsdangerous import Serializer
from rest_framework import status
from rest_framework.response import Response

from oauth import constants
from oauth.exceptions import QQAPIError

logger=logging.getLogger('django')
class OAuthQQ():
    #获取qq登录的网址
    def __init__(self,client_id=None,client_secret=None,redirect_uri=None,state=None):
        self.client_id=client_id or settings.QQ_CLIENT_ID
        self.client_secret=client_secret or settings.QQ_CLIENT_SECRET
        self.redirect_uri=redirect_uri or settings.QQ_REDIRECT_URI
        self.state=state or settings.QQ_STATE
    def get_qq_login_url(self):
        params={
            'response_type':'code',
            'client_id':self.client_id,
            'redirect_uri':self.redirect_uri,
            'state':self.state,
            'scope':'get_user_info'
        }
        url='https://graph.qq.com/oauth2.0/authorize?'+urlencode(params)
        return url

    def get_access_token(self,code):
        params={
            'grant_type':'authorization_code',
            'client_id':self.client_id,
            'client_secret':self.client_secret,
            'code':'code',
            'redirect':self.redirect_uri,
        }
        url='https://graph.qq.com/oauth2.0/token?'+urlencode(params)
        response=urlopen(url)
        response_data=response.read().decode()
        data=parse_qs(response_data)
        access_token=data.get('access_token',None)
        if not access_token:
            logger.error('code=%s msg=%s'%(data.get('code'),data.get('msg')) )
            raise QQAPIError
        return access_token

    def get_openid(self,access_token):
        url='https://graph.qq.com/oauth2.0/me?access_token='+access_token
        response=urlopen(url)
        response_data=response.read().decode()
        try:
            data=json.loads(response_data[10:-4])
        except Exception:
            data=parse_qs(response_data)
            logger.error('code=%s msg=%s' % (data.get('code'), data.get('msg')))
            raise QQAPIError
        openid=data.get('openid',None)
        return openid

    def generate_save_user_token(self,openid):
        serializer=Serializer(settings.SECRET_KEY,expires_in=constants.SAVE_QQ_USER_TOKEN_EXPIRES)
        data={'openid':openid}
        token=serializer.dumps(data)
        return token.decode()
